//
//  CarDetailViewModel.m
//  CarUser
//
//  Created by ayi on 2019/1/8.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "CarDetailViewModel.h"

#import "CustomSDScrollTableViewCell.h"
#import "Car_CarInfoTableViewCell.h"
#import "Car_CarFromInfoTableViewCell.h"
#import "Car_CarMoreTableViewCell.h"
#import "Car_CommentTableViewCell.h"
#import "Car_CollectionTableViewCell.h"

#import "Car_CarDetailViewController.h"
#import "UserRes.h"
#import "ChooseResultViewController.h"
#import <YYCategories/UITableView+YYAdd.h>
@interface CarDetailViewModel()<CarCollectionTableDelegate>

@property (nonatomic, strong) Custom_dznEmptyDelegate *custom_emptyView;

@end

@implementation CarDetailViewModel

@synthesize childtab = _childtab;
@synthesize childvc = _childvc;

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.responseModel = [[BaseNetResponseModel alloc]init];
        [self raccommandInitFirst];
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
    }
    return self;
}

- (void)setChildtab:(UITableView *)childtab{
    _childtab = childtab;
    
    @weakify(self);
    _custom_emptyView = [[Custom_dznEmptyDelegate alloc]initWithEmptyTitle:@"暂无数据" andImgName:nil andBtnName:nil andverticalOffset:0 andScroll:_childtab andCallBack:nil andFreshCallBack:^{
        @strongify(self);
        [self.raccommand execute:@(1)];
    } andOtherCallBack:nil];
    _custom_emptyView.adminationStr = @"加载中...";
}


- (void)raccommandInitFirst{

    @weakify(self);
    self.raccommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        
        [self.custom_emptyView willbeginStartAdmination];
        
        //数据
        RACSignal *sign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            if (self.SALECAR_ID == nil) {
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
                return nil;
            }
            
//            NSDictionary *params = @{
//                                     @"SALECAR_ID":self.SALECAR_ID,
//                                     };
            NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
            [param setObject:self.SALECAR_ID forKey:@"SALECAR_ID"];
            UserRes *user = [CommonMethod getUser];
            if (user) {
                [param setObject:user.mobile forKey:@"USERNAME"];
            }
            [self.custom_emptyView willbeginStartAdmination];
            
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getSaleCarInfo params:param md5Paraname:@"SALECAR_ID" request:nil response:self.responseModel success:^(id  _Nullable response) {
                
                self.model = [CarDetailMainModel yy_modelWithDictionary:response];
                self.modelDict = response;
                [subscriber sendNext:@"ok"];
                [subscriber sendCompleted];
            } fail:^(NSError * _Nonnull error) {
                
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
        //评论
        RACSignal *commentSign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSLog(@"评论获取-----------");
            if (self.SALECAR_ID == nil) {
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
                return nil;
            }
            
//            NSDictionary *params = @{
//                                     @"SALECAR_ID":self.SALECAR_ID
//                                     };
//
            NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
            [params setObject:self.SALECAR_ID forKey:@"SALECAR_ID"];
            UserRes *user = [CommonMethod getUser];
            if (user) {
                [params setObject:user.TOKEN forKey:@"DZTOKEN"];
            }
            
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getSaleCarCommentList params:params md5Paraname:@"SALECAR_ID" request:nil response:nil success:^(id  _Nullable response) {
                __block NSMutableArray *ary = [NSMutableArray arrayWithCapacity:0];
                [((NSArray *)response[@"list"]) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    CarCommentModel *onemodel = [CarCommentModel yy_modelWithDictionary:obj];
                    [ary addObject:onemodel];
                }];
                self.commentList = ary;
                
                [subscriber sendNext:@"ok"];
                [subscriber sendCompleted];
            } fail:^(NSError * _Nonnull error) {
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
        //推荐
        RACSignal *recommendSign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSLog(@"推荐获取-----------");
            if (self.SALECAR_ID == nil) {
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
                return nil;
            }
            
            NSDictionary *params = @{
                                     @"orderByStr":@"11"
                                     };
            
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getSaleCarList params:params md5Paraname:@"NULL" request:nil response:nil success:^(id  _Nullable response) {
                __block NSMutableArray *ary = [NSMutableArray arrayWithCapacity:0];
                [((NSArray *)response[@"list"]) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    CarSaleCarListModel *onemodel = [CarSaleCarListModel yy_modelWithDictionary:obj];
                    [ary addObject:onemodel];
                    if (idx == 4) {
                        *stop = YES;
                    }
                }];
                self.recommendList = ary;
                
                [subscriber sendNext:@"ok"];
                [subscriber sendCompleted];
            } fail:^(NSError * _Nonnull error) {
                [subscriber sendNext:@"error"];
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
        
        //组合
        RACSignal *groupSign = [[sign combineLatestWith:commentSign] combineLatestWith:recommendSign];
        
        
        return groupSign;
        
    }];
    
    //评论
    [[self.raccommand.executionSignals switchToLatest] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        
        NSLog(@"接口都完成调用");
        if (self.model != nil) {
            self.custom_emptyView.responseModel = nil;
        }else{
            self.custom_emptyView.responseModel = self.responseModel;
        }
        [self.childtab reloadData];
    }];
    
    
    //发送评论
    self.sendCommentRaccommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        RACSignal *sign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            UserRes *user  = [CommonMethod getUser];
            if (input == nil ||
                user.TOKEN == nil ||
                user.mobile == nil) {
                [subscriber sendCompleted];
                UserRes *user = [CommonMethod getUser];
                if (!user) {
                    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                    return nil;
                }
            }
            NSDictionary *params = @{
                                     @"TOKEN":user.TOKEN,
                                     @"USERNAME":user.mobile,
                                     @"PLNR00":input,
                                     @"ZSCXID":self.SALECAR_ID
                                     };
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_saveSaleCarCommentInfo params:params md5Paraname:@"TOKEN" request:nil response:nil success:^(id  _Nullable response) {
                
                NSDictionary *params = @{
                                         @"SALECAR_ID":self.SALECAR_ID
                                         };
                [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_getSaleCarCommentList params:params md5Paraname:@"SALECAR_ID" request:nil response:nil success:^(id  _Nullable response) {
                    __block NSMutableArray *ary = [NSMutableArray arrayWithCapacity:0];
                    [((NSArray *)response[@"list"]) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        CarCommentModel *onemodel = [CarCommentModel yy_modelWithDictionary:obj];
                        [ary addObject:onemodel];
                    }];
                    [self.sendmessageview sendSuccess];
                    self.commentList = ary;
                    [self.childtab reloadData];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.childtab scrollToRow:self.commentList.count > 0 ? self.commentList.count - 1 : 0 inSection:5 atScrollPosition:UITableViewScrollPositionBottom animated:YES];
                    });
                } fail:^(NSError * _Nonnull error) {
                }];
                
                
                [subscriber sendCompleted];
            } fail:^(NSError * _Nonnull error) {
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return sign;
    }];
    
    //点赞
    self.dianzanRaccommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self);
        RACSignal *sign = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            if (![input isKindOfClass:[CarCommentModel class]]) {
                [subscriber sendCompleted];
                return nil;
            }
            CarCommentModel *model = input;
//               UserRes *user  = [CommonMethod getUser];
//            NSDictionary *params = @{
//                                     @"SALECARCOMMENT_ID":model.SALECARCOMMENT_ID
//                                     };
            
            UserRes *user = [CommonMethod getUser];
            
            if (!user) {
                [[NSNotificationCenter defaultCenter]postNotificationName:NotificationCenterName_AlertLoginVC object:nil];
                 [subscriber sendCompleted];
                return nil;
                
            }
            
            NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
            [params setObject:model.SALECARCOMMENT_ID forKey:@"SALECARCOMMENT_ID"];
     
            
            if (user) {
                [params setObject:user.TOKEN forKey:@"TOKEN"];
            }
            [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_setSaleCarCommentDzs params:params md5Paraname:@"SALECARCOMMENT_ID" success:^(id  _Nullable response) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD showInfoWithStatus:@"点赞成功"];
                    //                [SVProgressHUD dismissWithDelay:10];
                });
                model.DZS000 = [NSString stringWithFormat:@"%ld",[model.DZS000 integerValue] + 1];
                model.DZFLAG = @"1";
                NSInteger row = [self.commentList indexOfObject:model];
                [self.childtab reloadRow:row inSection:5 withRowAnimation:UITableViewRowAnimationNone];
                [subscriber sendCompleted];
            } fail:^(NSError * _Nonnull error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD showErrorWithStatus:error.localizedDescription];
                });
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
        return sign;
    }];
}


#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.typeList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Car_DetailCellType type = [self.model.typeList[section] integerValue];
    switch (type) {
        case Car_DetailCellType_SD:
        case Car_DetailCellType_INFO:
        case Car_DetailCellType_Warranty:
        case Car_DetailCellType_Promise:
            return 1;
            break;
        case Car_DetailCellType_CarFromInfo:
            return 2;
            break;
        case Car_DetailCellType_Comment:
            return self.commentList.count > 0 ? self.commentList.count : 1;
            break;
        case Car_DetailCellType_More:
            return self.recommendList.count > 0 ? 1 : 0;
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Car_DetailCellType type = [self.model.typeList[indexPath.section] integerValue];
    switch (type) {
        case Car_DetailCellType_SD:
        {
            CustomSDScrollTableViewCell *cell = [CustomSDScrollTableViewCell initCellWithReuseIdentifier:@"CustomSDScrollTableViewCell" andTab:tableView];
            cell.listAry =  [self.model.list valueForKeyPath:@"@unionOfObjects.TPWJ00"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case Car_DetailCellType_INFO:
        {
            Car_CarInfoTableViewCell *cell = [Car_CarInfoTableViewCell initCellWithReuseIdentifier:@"Car_CarInfoTableViewCell" andTab:tableView];
            cell.model = self.model;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
            case Car_DetailCellType_CarFromInfo:
        {
            //车源信息
            if (indexPath.row == 0) {
                Car_CarFromInfoTableViewCell *cell = [Car_CarFromInfoTableViewCell initCellWithReuseIdentifier:@"Car_CarFromInfoTableViewCell" andTab:tableView];
                [cell addRac];
                cell.leftLab.text = @"车源所在地";
                cell.rightLab.text = self.model.cardetailInfoModel.CYSZD;
                cell.moreBtn.hidden = NO;
//                [cell.moreBtn setTitle:@"查运价" forState:UIControlStateNormal];
                cell.moreBtn.hidden = YES;
                cell.lineview.hidden = NO;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }else{
                Car_CarFromInfoTableViewCell *cell = [Car_CarFromInfoTableViewCell initCellWithReuseIdentifier:@"Car_CarFromInfoTableViewCell" andTab:tableView];
                [cell addRac];
                cell.leftLab.text = @"手续";
                cell.rightLab.text = self.model.cardetailInfoModel.SX0000;
                cell.moreBtn.hidden = YES;
                cell.lineview.hidden = YES;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }
        }
            break;
        case Car_DetailCellType_Warranty:
        {
            //质保方案
            Car_CarMoreTableViewCell *cell = [Car_CarMoreTableViewCell initCellWithReuseIdentifier:@"Car_CarMoreTableViewCell" andTab:tableView];
            [cell resetModel:self.model withType:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case Car_DetailCellType_Promise:
        {
            Car_CarMoreTableViewCell *cell = [Car_CarMoreTableViewCell initCellWithReuseIdentifier:@"Car_CarMoreTableViewCell" andTab:tableView];
            [cell resetModel:self.model withType:2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case Car_DetailCellType_Comment:
        {
            if (self.commentList.count == 0 && indexPath.row == 0) {
                Car_CommentDefalutTableViewCell *cell = [Car_CommentDefalutTableViewCell initCellWithReuseIdentifier:@"Car_CommentDefalutTableViewCell" andTab:tableView];
                return cell;
            }
            Car_CommentTableViewCell *cell = [Car_CommentTableViewCell initCellWithReuseIdentifier:@"Car_CommentTableViewCell" andTab:tableView];
            cell.model = self.commentList[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            @weakify(self);
            [cell setCommentZanBtnCallBck:^(CarCommentModel * _Nonnull model) {
                @strongify(self);
                [self.dianzanRaccommand execute:model];
            }];
            return cell;
        }
            break;
        case Car_DetailCellType_More:
        {
            Car_CollectionTableViewCell*cell = [Car_CollectionTableViewCell initWithReuseIdentifier:@"Car_CollectionTableViewCell" andTab:tableView];
            cell.listAry = self.recommendList;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.collectionselectedDelegate = self;
            return cell;
        }
            break;
        default:
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellDefault"];
            if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellDefault"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
    }
    
}




#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Car_DetailCellType type = [self.model.typeList[indexPath.section] integerValue];
    switch (type) {
        case Car_DetailCellType_SD:
            return 225;
            break;
        case Car_DetailCellType_INFO:
        case Car_DetailCellType_Comment:
        case Car_DetailCellType_More:
            return UITableViewAutomaticDimension;
            break;
        case Car_DetailCellType_CarFromInfo:
            return 44;
            break;
        case Car_DetailCellType_Warranty:
            return 76;
            break;
        case Car_DetailCellType_Promise:
            return 44;
            break;
        default:
            return 0;
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)handleMoreAction
{
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.type = @"1";
    controller.pushType = 1;
    [self.childvc.navigationController pushViewController:controller animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return nil;
    }
    UIView *header = [[UIView alloc]init];header.backgroundColor = UIColor.whiteColor;
    
    UIView *colorview = [[UIView alloc]init];colorview.backgroundColor = Common_TextLowColor;
    colorview.layer.masksToBounds = YES;
    colorview.layer.cornerRadius = 3;
    UILabel *titleLab = [UILabel customInitLabWithFont:16 andTextColor:UIColor.blackColor andAlign:NSTextAlignmentLeft];
    UIButton *moreBtn = ({
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.titleLabel.font = [UIFont systemFontOfSize:14];
//        [btn setTitleColor:Common_TextLowColor forState:UIControlStateNormal];
//        [btn setTitle:@"更多 >" forState:UIControlStateNormal];
//        btn;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"更多" forState:UIControlStateNormal];
        [btn setImage:IMG(@"home_arrow") forState:UIControlStateNormal];
        [btn setTitleColor:Common_TextLowColor forState:UIControlStateNormal];
        btn.titleLabel.font = FONT_SIZE(14);
        [btn addTarget:self action:@selector(handleMoreAction) forControlEvents:UIControlEventTouchUpInside];
        //        _a_detaiBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        //        //    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0 ,0, 0.0,0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
        [btn setImageEdgeInsets:UIEdgeInsetsMake( 0, 0,0.0, -90)];//图片距离右边框距离减少图片的宽度，其它不边
        btn;
        
    });
    moreBtn.hidden = YES;
    
    [header addSubview:colorview];
    [header addSubview:titleLab];
    [header addSubview:moreBtn];
    
    colorview.frame = CGRectMake(15, 10, 5, 17);
    titleLab.frame = CGRectMake(CGRectGetMaxX(colorview.frame) + 6, 10, BOUNDWIDTH - CGRectGetMaxX(colorview.frame) - 6 -15, 17);
    moreBtn.frame = CGRectMake(BOUNDWIDTH - 8 - 60, 0, 60, 44);
    
    Car_DetailCellType type = [self.model.typeList[section] integerValue];
    switch (type) {
        case Car_DetailCellType_CarFromInfo:
            titleLab.text = @"车源信息";
            break;
        case Car_DetailCellType_Warranty:
            titleLab.text = @"质保方案";
            break;
        case Car_DetailCellType_Promise:
            titleLab.text = @"购车承诺";
            break;
        case Car_DetailCellType_Comment:
            titleLab.text = @"车友评价";
            break;
        case Car_DetailCellType_More:
            titleLab.text = @"推荐车源";
            moreBtn.hidden = NO;
            break;
        default:
            titleLab.text = @"";
            break;
    }
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    Car_DetailCellType type = [self.model.typeList[section] integerValue];
    switch (type) {
        case Car_DetailCellType_Comment:
        {
            UIView *footer = [[UIView alloc]init];footer.backgroundColor = Common_BackgroundGrayColor;
            
            UIView *alertCommentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 60)];
            alertCommentView.backgroundColor = UIColor.whiteColor;
            UIButton *alertCommentBtn =({
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.titleLabel.font = FONT_SIZE(14);
                btn.backgroundColor = Common_Head_Color;
                [btn setTitle:@"我有话要说" forState:UIControlStateNormal];
                [btn setImage:IMG(@"ic_car_detail_comment") forState:UIControlStateNormal];
                [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
                btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
                btn.layer.masksToBounds = YES;
                btn.layer.cornerRadius = 20;
                [btn addTarget:self action:@selector(alertCommetnAction) forControlEvents:UIControlEventTouchUpInside];
                btn;
            });
            [alertCommentView addSubview:alertCommentBtn];
            alertCommentBtn.frame = CGRectMake(15, 8, BOUNDWIDTH - 30, 40);
            
            [footer addSubview:alertCommentView];
            
            return footer;
        }
            break;
        default:
        {
            UIView *footer = [[UIView alloc]init];footer.backgroundColor = Common_BackgroundGrayColor;
            return footer;
        }
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return .5f;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return .5f;
    }
    Car_DetailCellType type = [self.model.typeList[section] integerValue];
    switch (type) {
        case Car_DetailCellType_Comment:
            return 70;
            break;
        default:
            return 10;
            break;
    }
}

#pragma mark ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)vm_collectionSelected:(CarSaleCarListModel *)model{
    if (model == nil) {
        return;
    }
    Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];
    push.SERIES_ID = model.SALECAR_ID;
    [self.childvc.navigationController pushViewController:push animated:YES];
}




//弹出输入框
- (void)alertCommetnAction{
    [self.sendmessageview becomeFirstResponder];
    [self.childtab scrollToRow:self.commentList.count > 0 ? self.commentList.count-1 : 0 inSection:5 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}





@end




@implementation CarDetailMainModel


+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"cardetailInfoModel":@"info",
             };
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
             @"aqList":[CarConfigurationModel class],
             @"ckList":[CarConfigurationModel class],
             @"ssList":[CarConfigurationModel class],
             @"ysList":[CarConfigurationModel class],
             @"list":[CarImgModel class]
             };
}

+ (nullable NSArray<NSString *> *)modelPropertyBlacklist{
    return @[
             @"typeList",
             @"hasChoose"
             ];
}


- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic{
    
    self.typeList = @[
                      @(Car_DetailCellType_SD),
                      @(Car_DetailCellType_INFO),
                      @(Car_DetailCellType_CarFromInfo),
                      @(Car_DetailCellType_Warranty),
                      @(Car_DetailCellType_Promise),
                      @(Car_DetailCellType_Comment),
                      @(Car_DetailCellType_More)
                      ].mutableCopy;
    
    return YES;
}


+ (BOOL )addToArchiverWithMolde:(CarDetailMainModel *)model{
    return [self addToArchiverWithAry:@[model]];
}

#define ContrastFileUrl [[FileUtils getDocPath] stringByAppendingString:@"/contrastCars.archiver"]

+ (BOOL )addToArchiverWithAry:(NSArray<CarDetailMainModel *> *)ary{
    
    NSString *fileUrl = ContrastFileUrl;
    
//    return [FileUtils deleteFileAtPath:fileUrl];
    
    id oneary = [NSKeyedUnarchiver unarchiveObjectWithFile:fileUrl];
    __block BOOL oneHasHistory = NO;
    if ([oneary isKindOfClass:[NSArray class]]) {
        NSMutableArray<CarDetailMainModel *> *archiverAry = [NSMutableArray arrayWithArray:((NSArray *)oneary)];
        NSMutableArray<CarDetailMainModel *> *archiverAry2 = [NSMutableArray arrayWithCapacity:0];
        
        [ary enumerateObjectsUsingBlock:^(CarDetailMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            __block CarDetailMainModel *oneModel;
            __block NSNumber *index;
            [((NSArray *)oneary) enumerateObjectsUsingBlock:^(CarDetailMainModel *obj2, NSUInteger idx2, BOOL * _Nonnull stop2) {
                if ([obj2.cardetailInfoModel.SALECAR_ID isEqualToString:obj.cardetailInfoModel.SALECAR_ID]) {
                    oneModel = obj2;
                    index = @(idx2);
                    *stop = YES;
                }
            }];
            if (oneModel != nil) {
                if (ary.count == 1) {
                    oneHasHistory = YES;
                }
                [archiverAry replaceObjectAtIndex:[index integerValue] withObject:oneModel];
            }else{
                [archiverAry2 addObject:obj];
            }
        }];
        [archiverAry addObjectsFromArray:archiverAry2];
        
        BOOL isOK = [NSKeyedArchiver archiveRootObject:archiverAry toFile:fileUrl];
        if (oneHasHistory) {
            return NO;
        }
        return isOK;
        
    }else{
        
        return [NSKeyedArchiver archiveRootObject:ary toFile:fileUrl];
        
    }
    
}

+ (BOOL )hasChooseContrastWithID:(NSString *)SALECAR_ID{
    id oneary = [NSKeyedUnarchiver unarchiveObjectWithFile:ContrastFileUrl];
    if ([oneary isKindOfClass:[NSArray class]]) {
        
        __block BOOL haveChoose = NO;
        [((NSArray *)oneary) enumerateObjectsUsingBlock:^(CarDetailMainModel *obj2, NSUInteger idx2, BOOL * _Nonnull stop2) {
            if ([obj2.cardetailInfoModel.SALECAR_ID isEqualToString:SALECAR_ID]) {
                haveChoose = YES;
                *stop2 = YES;
            }
        }];
        return haveChoose;
    }
    return NO;
}

+ (NSMutableArray *)contrastList{
    id oneary = [NSKeyedUnarchiver unarchiveObjectWithFile:ContrastFileUrl];
    if ([oneary isKindOfClass:[NSArray class]]) {
        return ((NSArray *)oneary).mutableCopy;
    }
    return nil;
}

+ (BOOL)clearContrastList{
    NSString *fileUrl = ContrastFileUrl;
    
    return [FileUtils deleteFileAtPath:fileUrl];
}


@end
